<?php
/**
 * Created by PhpStorm.
 * User: xunuo
 * Date: 2017/6/26
 * Time: 10:15
 */

namespace app\company\controller;

use think\Exception;

/**
 * 用户控制器
 * @package app\admin\controller
 */
class User extends Common
{
    /**
     * 修改密码
     * @return \think\response\View
     */
    public function modifyPassword()
    {
        return view();
    }

    /**
     * 执行修改密码
     * @return \think\response\Json
     */
    public function doModifyPassword()
    {
        $password = input('post.password');
        $newPassword = input('post.newPassword');
        $userModel = new \app\admin\model\User();
        try {
            $userModel->doModifyPassword(session('company.id'), $password, $newPassword);
        } catch (Exception $e) {
            return json([
                'statusCode' => 300,
                'message' => $e->getMessage(),
            ]);
        }
        return json([
            'statusCode' => 200,
            'message' => '修改成功',
            'navTabId' => '',
            'callbackType' => 'closeCurrent',
        ]);
    }

    /**
     * 修改个人信息
     */
    public function member()
    {
        $user_id = session('company.user_id');
        $info = db('users')->where('user_id', $user_id)->find();
        return view('', $info);
    }

    public function doMember()
    {
        $data = input('post.');
        $data['update_time'] = time();
        $res = db('users')->update($data);
        if ($res) {
            return json([
                'statusCode' => 200,
                'message' => '修改成功',
                'navTabId' => '',
                'callbackType' => 'closeCurrent',
            ]);
        } else {
            return json([
                'statusCode' => 300,
                'message' => '修改失败',
            ]);
        }

    }
    public function member_mobile()
    {
        $user_id = session('company.user_id');
        $info = db('users')->where('user_id', $user_id)->find();
        return view('', $info);
    }

    public function doMember_mobile()
    {
        $data = input('post.');
        $Users = new \app\logic\model\Users;
        try {
            $list = $Users->modifymobile($data, $data['user_id'], true);
        } catch (Exception $e) {
            return json([
                'statusCode' => 300,
                'message' => $e->getMessage(),
            ]);
        }
        return json([
            'statusCode' => 200,
            'message' => '修改成功',
            'navTabId' => '',
            'callbackType' => 'closeCurrent',
        ]);
    }
    /**
     * 获取验证码
     */
    public function doCode()
    {
        $data = input('post.');
        if (isset($data['register'])) {
            $register = $data['register'];
        }
        $Users = new \app\logic\model\Users();
        try {
            $list = $Users->docode($data['phone'], $register);
        } catch (\Exception $e) {
            return ['result' => 1, 'message' => $e->getMessage()];
        }
        return ['result' => 2, 'message' => '发送成功'];
    }
}